﻿using System.Collections.Generic;

namespace SDT.CodeSystem
{
    /// <summary>
    /// Written by SinhNT
    /// Ref: http://vnexpress.net/GL/Kinh-doanh/2009/11/3BA154A2/
    /// </summary>
    public static class MobileProviderCodes
    {
        public enum Codes
        {
            VinaPhone, MobiFone, Viettel, SFone, EvnTel, Vietnamobile, Beeline, IndochinaTel, FptTel, VtcTel,
            NA = 100
        };

        public static string[] StrCodes = new string[]
        {
            "VinaPhone", "MobiFone", "Viettel", "SFone", "EvnTel", "Vietnamobile", "Beeline", "IndochinaTel", "FptTel", "VtcTel",
            "NA"
        };

        internal static string[][] Prefixes = new string[][]
        {
            new string[] { "091", "094", "0123", "0125", "0127", "0129" },
            new string[] { "090", "093", "0121", "0122", "0126", "0128", "0120" },
            new string[] { "098", "097", "0165", "0166", "0167", "0168", "0169", "0164" },
            new string[] { "095" },
            new string[] { "096" },
            new string[] { "092" },
            new string[] { "0199" },
            new string[] { },
            new string[] { },
            new string[] { }
        };

        /// <summary>
        /// 10 digits phone number
        /// </summary>
        private static readonly string NumberFormat1 = "XXXXXXXXXX";
        /// <summary>
        /// 11 digits phone number
        /// </summary>
        private static readonly string NumberFormat2 = "XXXXXXXXXXX";

        private static readonly string Prefix1 = "09X";
        //private static readonly string Prefix2 = "01XX";

        public static bool PreCheck(string phoneNumber)
        {
            foreach (var l in Prefixes)
                foreach (var i in l)
                    if (phoneNumber.StartsWith(i))
                        return true;
            return false;
        }

        public static bool IsValid(string phoneNumber, Codes provider)
        {
            if (phoneNumber.Length == NumberFormat1.Length || phoneNumber.Length == NumberFormat2.Length)
            {
                if (provider == Codes.NA)
                {
                    foreach (var l in Prefixes)
                        foreach (var i in l)
                            if (phoneNumber.StartsWith(i))
                                return i.Length == Prefix1.Length ? 
                                    phoneNumber.Length == NumberFormat1.Length : phoneNumber.Length == NumberFormat2.Length;
                }
                else
                {
                    var l = Prefixes[(int)provider];
                    foreach (var i in l)
                        if (phoneNumber.StartsWith(i))
                            return i.Length == Prefix1.Length ?
                                phoneNumber.Length == NumberFormat1.Length : phoneNumber.Length == NumberFormat2.Length;
                }
            }
            return false;
        }

        public static bool IsValid(string phoneNumber)
        {
            return IsValid(phoneNumber, Codes.NA);
        }

        public static string ToString(Codes c)
        {
            return StrCodes[(int)c];
        }

        public static Codes FromString(string strCode)
        {
            for (int i = 0; i < StrCodes.Length; i++)
			{
                if (strCode.Trim().ToUpper() == StrCodes[i])
                    return (Codes)i;
			}

            return Codes.NA;
        }
    }
}